package com.neusoft.elm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.elm.po.User;
import com.neusoft.elm.service.UserService;
import com.neusoft.elm.service.impl.UserServiceImpl;

public class UserController {
    public UserController() {
    }

    public Object saveUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        String userName = request.getParameter("userName");
        Integer userSex = Integer.parseInt(request.getParameter("userSex"));
        User user = new User(userId, password, userName, userSex,null);
        UserService service = new UserServiceImpl();
        int result = service.saveUser(user);
        return result;
    }

    public Object getUserByIdByPass(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        UserService service = new UserServiceImpl();
        User user = service.getUserByIdByPass(userId, password);
        if (user != null) {
            request.getSession().setAttribute("user", user);
        }
        return user;
    }

    public Object getUserById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userId = request.getParameter("userId");
        UserService service = new UserServiceImpl();
        User user = service.getUserById(userId);
        return user;
    }
    public int updateUser(HttpServletRequest request, HttpServletResponse response) {
    	UserService service = new UserServiceImpl();
    	String userId = request.getParameter("userId");
        String password = request.getParameter("userName");
        String userName = request.getParameter("password");
        Integer userSex = service.getUserById(userId).getUserSex();
        User user = new User(userId, password, userName, userSex,null);
        int result = service.updateUser(user);
        System.out.println("userId = " + user.getUserId());
        System.out.println("userName = " + user.getUserName());
        System.out.println("userPassword = " + user.getPassword());
        return result;
    }
}